﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SQLite;
//using Demo_HHT_EARTH.DLflight;
namespace Demo_HHT_EARTH
{
    public partial class EMcheckin : Form
    {
        DataSet Ds = new DataSet();
        DataTable Table_Emp = new DataTable();
        DataGrid G_test = new DataGrid();
        string empgroupname, strempcode = string.Empty;
        SQLiteConnection cn2 = new SQLiteConnection();
        public EMcheckin(string empgroupname)
        {
            
            InitializeComponent();
           this.empgroupname = empgroupname;
           textBox2.Focus();
           createDatagrid();
            
            
        }
      
        private DataTable CreateTable_Emp(DataSet Ds)
        {
            Table_Emp = new DataTable();
            Table_Emp.Columns.Add("Employee ID");
            Table_Emp.Columns.Add("In");
            Table_Emp.Columns.Add("Status");
            Table_Emp.Columns.Add("Employee Name");

            ///Table_Emp.Rows.
            // Table_Emp.Rows.Add(
            for (int i = 0; i < Ds.Tables[0].Rows.Count ; i++)
            {
                string emp_id = Ds.Tables[0].Rows[i]["employee_code"].ToString();
               
                string emp_ck = Ds.Tables[0].Rows[i]["check_in_flag"].ToString();
                string emp_status = Ds.Tables[0].Rows[i]["status"].ToString();
                string emp_name = Ds.Tables[0].Rows[i]["employee_display_name"].ToString();
                if (emp_ck.Trim() == "YES")
                {
                    emp_ck = "   X   "; 
                  // emp_ck = String.Format("{5,5}",emp_ck).ToString();//emp_ck = StringAlignment.Center;
                }
                else if (emp_ck.Trim() == "NO")
                {
                    emp_ck = "";
                }

                Table_Emp.Rows.Add(emp_id, emp_ck, emp_status, emp_name);
            }
          
           //Table_Emp.
            
            return Table_Emp;
        }
      

        private void menuItem1_Click(object sender, EventArgs e)
        {
            //this.Close();
            //DEMO_TEMP1.MainMenu Mn = new DEMO_TEMP1.MainMenu(true, 2);
            //Mn.Show();
            Demo_HHT_EARTH.EMcheckInGroup EMG = new EMcheckInGroup("");
            EMG.Show();
            this.Close();
        }

        private void menuItem2_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(textBox1.Text);
            //this.Close();
            //EMcheckInGroup EM3 = new EMcheckInGroup();
            //EM3.Show();
            //this.Close();
            //DEMO_TEMP1.MainMenu Mn = new DEMO_TEMP1.MainMenu(true, 2);
            //Mn.Show();
            textBox2.Focus();
            createDatagrid();

           
        }

       
        private void WorkScan(string strempcode)
        {
            Demo_HHT_EARTH.EMcheckInGroup EKG = new EMcheckInGroup("");

            bool ck_codeem = EKG.ValidateCode(ref strempcode); //ValidateCode(strempcode);
            int check_in = EKG.ValidatedEmp_Code(strempcode);
            Demo_HHT_EARTH.DataGetEmpCheckIn.emp_code = strempcode;
            if (ck_codeem)
            {
                if (check_in==1)
                {
                    UpdateEmp_code(strempcode);
                    textBox2.Focus();
                }
                else if(check_in==-1)
                {
                    Demo_HHT_EARTH.CheckInConfirmTemp Emp_ConfTemp = new CheckInConfirmTemp(0);
                    Emp_ConfTemp.Show();

                }
                else if (check_in == 0)
                {
                    label1.Visible = true;
                    label1.Text = "Check In Employee Duplicate";
                    textBox2.Focus();
                }
                textBox2.Text = String.Empty;
                
            }
            else
            {
                label1.Visible = true;
                label1.Text = "Employee Code Error";
                textBox2.Focus();
            }
            

        }

        private void UpdateEmp_code(string strempcode)
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
            }
            catch (Exception)
            {

                MessageBox.Show("notconect SQLite");
            }

            string strcomm = "Update t_earth_flight_employee Set check_in_flag = 'YES',status ='ASSIGN' where employee_code = @strempcode";
            SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
            COMM.Parameters.AddWithValue("@strempcode", strempcode);
            SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
            cn2.Open();
            COMM.ExecuteNonQuery();
            
            cn2.Close();
            createDatagrid();
        }

        //private void RefreshDatagrid()
        //{
        //    try
        //    {
        //        cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
        //        cn2.Open();
        //        string strconinsert = "Select * from t_earth_flight_employee where employee_group =@empgroupname order by check_in_flag";
        //        SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
        //        command.Parameters.AddWithValue("@empgroupname", empgroupname);
        //        command.Connection = cn2;
        //        command.ExecuteNonQuery();

        //        SQLiteDataAdapter DA_local = new SQLiteDataAdapter(command);
        //        Ds = new DataSet();
        //        cn2.Close();
        //        DA_local.Fill(Ds, "Local");

        //        if (Ds == null)
        //        {
        //            MessageBox.Show("No have data SQLite");
        //        }
        //        else
        //        {
        //            Table_Emp = RefreshTable_Emp(Ds);

        //            dataGrid1.DataSource = Table_Emp;




        //        }

        //    }
        //    catch (Exception)
        //    {

        //        MessageBox.Show("notconect SQLite");
        //    }
        //}

        //private DataTable RefreshTable_Emp(DataSet Ds)
        //{
           
        //}

        private void createDatagrid()
        {
            try
            {
                cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
                cn2.Open();
                string strconinsert = "Select * from t_earth_flight_employee where employee_group =@empgroupname order by check_in_flag";
                SQLiteCommand command = new SQLiteCommand(strconinsert, cn2);
                //cmd.Parameters.AddWithValue("@mawb_no", mawb_no);
                command.Parameters.AddWithValue("@empgroupname", empgroupname);
                command.Connection = cn2;
                command.ExecuteNonQuery();

                SQLiteDataAdapter DA_local = new SQLiteDataAdapter(command);
                Ds = new DataSet();
                cn2.Close();
                DA_local.Fill(Ds, "Local");

                if (Ds == null)
                {
                    MessageBox.Show("No have data SQLite");
                }
                else
                {
                    Table_Emp = CreateTable_Emp(Ds);

                    dataGrid1.DataSource = Table_Emp;




                }

            }
            catch (Exception )
            {

                MessageBox.Show("notconect SQLite");
            }
        }
        DataSet testT = new DataSet();
        //private bool ValidatedEmp_Code(string strempcode)
        //{
        //    try
        //    {
        //        cn2.ConnectionString = "Data Source=|DataDirectory|db.sqlite";
        //    }
        //    catch (Exception)
        //    {

        //        MessageBox.Show("notconect SQLite");
        //    }
        //    string strcomm = "SELECT * FROM t_earth_flight_employee where employee_code = @strempcode";
        //    SQLiteCommand COMM = new SQLiteCommand(strcomm, cn2);
        //    COMM.Parameters.AddWithValue("@strempcode", strempcode);
        //    SQLiteDataAdapter Lda = new SQLiteDataAdapter(COMM);
        //    testT = new DataSet();
        //    cn2.Open();
        //    Lda.Fill(testT, "B");
        //    cn2.Close();
        //    if (testT.Tables[0].Rows.Count > 0)
        //    {
        //        dataGrid1.DataSource = null;
        //        dataGrid1.DataSource = testT.Tables[0];
        //        return true;

        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar.ToString() != "\r")
            {
                strempcode += e.KeyChar.ToString();
            }
            else if (e.KeyChar.ToString() == "\r")
            {
                WorkScan(strempcode);
                strempcode = "";
            }
            
        }
    }


    //public class DatagridCheckInColumn : DataGridColumnStyle
    //{
    //    public RadioButton CheckIn = new RadioButton();

    //    public DatagridCheckInColumn():base()
    //    {
    //        CheckIn.Visible = true;
    //    }

    //}
}